﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPMigrationOracleUtility
{
    public partial class EditMapplingForm : Form
    {
        public EditMapplingForm()
        {
            InitializeComponent();
            string SiteUrl = Convert.ToString(SPMigrationOracleUtility.Appl.Default.SiteURL);
            txtUrl.Text = SiteUrl;
        }

        public string mapid;
        public string mode;


        Timer timer1 = new Timer();
        //view_mapping_lists objMode = new view_mapping_lists();
        Map_Migration objImp = new Map_Migration();
        mapping_calss objMap = new mapping_calss();
        cls_mapping obj = new cls_mapping();
        Collect_Site obj1 = new Collect_Site();
        orcConnection objCon = new orcConnection();

        DataTable dtResults;
        DataTable dt;
        DataTable dt1;
        // mapping_posts objBind = new mapping_posts();
        int cnt;

        bool flag = false;
        bool flg;
        bool pnl3flag;
        bool flagMapInit;
        bool flagSites;
        bool flagGv;
        bool flagview;
        bool flagcgintabl;
        //string mode;

        bool srctblchanged = false, desttblchanged = false;
        int ListSelectioncnt = 0;
        int Libraryselcount = 0;

        private void EditMapplingForm_Load(object sender, EventArgs e)
        {

            if (mode == "View")
            {
                pnl_disable();
            }


            //mani kandan...

            this.Height = 450;
            this.Width = 800;
            obj.Panel_Prop(pnlMappingname);
            obj.Panel_Prop(pnlDBList);
            obj.Panel_Prop(pnlURL);
            obj.Panel_Prop(pnlGrid);
            obj.Panel_Prop(pnlGridBind);
            obj.Panel_Prop(pnlIntegrate);
            obj.Panel_Prop(pnlDataMigrate);

            lstMapListLib.DisplayMember = "Text";                     
            lblDisp.Text = "Oracle and SharePoint Mapping Step-1 - Create Mapping Name";

            objMap.Mapping_List_Retrieve(mapid); // mapping_list
            txtMappingName.Text  = objMap.mapname;
            txtMappingDesc.Text = objMap.mapdesc;
            //lstMapDB.Items.Clear();
            objMap.GetUrl(mapid);

            if (objMap.allitems == "True" && objMap.viewname == "")
            {
                obj.ShowAllTables(lstMapDBs);
                rdBtnTables.Checked = true;
                rdBtnAll.Checked = true;
                //txtQuery.Visible = false;
                string strQry = "select * from " + objMap.tblname;
                obj.LoadOrcData(strQry, gvBind);
                //gvBind.Location = new Point(3, 66);
                //gvBind.Height= 200;
               // gvBind.Width = 720;
                btnExecute.Visible = false;

            }
            else if (objMap.allitems == "False")
            {

                obj.ShowAllTables(lstMapDBs);
                //rdBtnTables.Checked = true;
                //rdBtnAll.Checked = true;
                rdBtnQuery.Enabled = true;
                rdBtnQuery.Checked = true;
                txtQuery.Visible = true;
                txtQuery.Text = objMap.sqlquery;
                string strQry = objMap.sqlquery;
                obj.LoadOrcData(strQry, gvBind);
                //gvBind.Location = new Point(3, 66);
                //gvBind.Height = 200;
                //gvBind.Width = 720;
                btnExecute.Visible = true;
            }
            else 
            {

                obj.ShowAllViews(lstMapDBs);
                
                rdBtnAll.Checked = true;
                txtQuery.Visible = false;
                string strQry = "select * from " + objMap.viewname;
                obj.LoadOrcData(strQry, gvBind);
                //gvBind.Location = new Point(3, 66);
                //gvBind.Height = 200;
               // gvBind.Width = 720;
                btnExecute.Visible = false;
                flagview = true;
                rdBtnViews.Checked = true;
            }


           // objMap.GetUrl(mapid);

            flagSites = true;
            obj1.GetSubSites(cmbSites,objMap.webUrl);
            if (cmbSites.Items.Count > 0)
            {
                cmbSites.SelectedIndex = 0;
            }
            
            ListItemPair oweb = (ListItemPair)cmbSites.SelectedItem;
            obj1.GetLists(lstLibList, cmbSites, objMap.webUrl);
            //clr_MapList();

            objMap.Mapping_Col_List_Edit(mapid, lstMapDBs, lstMapDB, lstLibList, lstMapListLib); // mapping_column lsit

            flg = false;
            if (rdBtnTables.Checked)
            {
                btn_DB_Enable(rdBtnTables);

            }
            else
            {
                btn_DB_Enable(rdBtnViews);
            }

            ListSelectioncnt = objMap.lstTemCnt;
            Libraryselcount = objMap.libTemCnt;
            List_Lib_Ck();

            flagGv = true;
           // objMap.mapping_coll_name_bind(dg1, objMap.webUrl, mapid);
            dt = objMap.mapping_coll_name_bind(dg1, objMap.SiteUrl, mapid);
            dt1 = dt;
            if (gvBind.DataSource == null) { btnNextPnl5.Enabled = false; } else { btnNextPnl5.Enabled = true; }
          
        }

        private void pnl_disable()
        {

            foreach (Control c in this.Controls)
                foreach (Control ctrl in c.Controls)
                {
                    ctrl.Enabled = false;
                    if (ctrl is Button)
                    {
                        ((Button)ctrl).Enabled = true;
                    }

                }
            // txtQuery.Enabled = false;
            btnDbForward.Enabled = false;
            btnDbBackward.Enabled = false;
            btnGo.Enabled = false;
            btnListLibAdd.Enabled = false;
            btnListLibRem.Enabled = false;
            //btnClear.Enabled = false;
            btnAddGrid.Enabled = false;
            btnExecute.Enabled = false;
            dg1.Enabled = true;
            gvBind.Enabled = true;
        }
        public void btn_DB_Enable(RadioButton rd)
        {
            string str = rd.Name;

            cnt = lstMapDB.Items.Count;

            if (str == "rdBtnTables" || str == "optTables")
            {

                if (cnt == 2)
                {
                    btnDbForward.Enabled = false;
                    btnDbBackward.Enabled = true;
                    btnNextPnl3.Enabled = true;
                }

                if (cnt == 1)
                {
                    btnDbForward.Enabled = true;
                    btnDbBackward.Enabled = true;
                    btnNextPnl3.Enabled = true;

                }

            }

            else
            {

                if (cnt == 1)
                {
                    btnDbForward.Enabled = false;
                    btnDbBackward.Enabled = true;
                    btnNextPnl3.Enabled = true;
                }
                else
                {
                   // btnDbForward.Enabled = true;
                }
            }

          

        }

        private void List_Lib_Ck()
        {

            if (ListSelectioncnt == 0 && Libraryselcount == 1)
            {
                btnListLibAdd.Enabled = true;
            }
            else if (ListSelectioncnt == 1 && Libraryselcount == 0)
            {
                btnListLibAdd.Enabled = true;
            }
            else if (ListSelectioncnt == 2 && Libraryselcount == 0)
            {
                btnListLibAdd.Enabled = false;
            }
            else if (ListSelectioncnt == 1 && Libraryselcount == 1)
            {
                btnListLibAdd.Enabled = false;
            }
            else if (ListSelectioncnt == 0 && Libraryselcount == 0)
            {
                btnListLibRem.Enabled = false;
            }

            Step4Enable();
        }
       
        public void Step4Enable()
        {
            if (ListSelectioncnt > 0 || Libraryselcount > 0)
            {
                btnNextPnl3.Enabled = true;
                btnListLibRem.Enabled = true;
            }
            else
            {
                btnListLibAdd.Enabled = true;
                btnNextPnl3.Enabled = false;
                btnListLibRem.Enabled = false;
            }
        }

        public void Step4Enable_Post()
        {
            if (objMap.lstTemCnt > 0 || objMap.libTemCnt   > 0)
            {
                ListSelectioncnt = objMap.lstTemCnt;
                btnNextPnl3.Enabled = true;
                btnListLibRem.Enabled = true;
            }
            else
            {
                Libraryselcount = objMap.libTemCnt;
                btnListLibAdd.Enabled = true;
                btnNextPnl3.Enabled = false;
                btnListLibRem.Enabled = false;
            }
        }

        private void txtMappingName_KeyDown(object sender, KeyEventArgs e)
        {
            if (Convert.ToInt32(e.KeyValue) == 13)
            {
                txtMappingDesc.Focus();
                btnNextPnl1.Enabled = true;

            }
            else
            {

            }
        }

        private void txtMappingDesc_KeyDown(object sender, KeyEventArgs e)
        {

            btnNextPnl1.Enabled = true;
            if (Convert.ToInt32(e.KeyValue) == 13)
            {

                btnNextPnl1.Focus();
            }
            else
            {

            }
        }

        private void btnNextPnl1_Click(object sender, EventArgs e)
        {
            Pnl2_Transaction();
            if (mode == "View")
            { btnDbBackward.Enabled = false; btnDbForward.Enabled = false; btnListLibAdd.Enabled = false; btnListLibRem.Enabled = false; }
            if (lstMapDB.Items.Count > 0) { btnNextPnl2.Enabled = true; } else { btnNextPnl2.Enabled = false; }
        }

        private void Pnl2_Transaction()
        {
            flag = false;
            lblDisp.Text = "Oracle and SharePoint Mapping Step-2 - Select Oracle Mapping List";
            pnlDBList.Visible = true;
            pnlMappingname.Visible = false;
            
           // btnDbBackward.Enabled = false;
            
            btnDbForward.Enabled = true;

            if (rdBtnTables.Checked)
            {
                if (lstMapDB.Items.Count == 2 && flg == true)
                {
                    btnDbBackward.Enabled = true;
                    btnDbForward.Enabled = false;
                }
                else if (lstMapDB.Items.Count == 1 && flg == true)
                {
                    btnDbForward.Enabled = true;
                    btnDbBackward.Enabled = true;
                }
                else if (lstMapDB.Items.Count == 2 && flg == false)
                {
                    btnDbForward.Enabled = false;
                    btnDbBackward.Enabled = true;
                    btnNextPnl2.Enabled = true;
                    flg = true;
                }
                else if (lstMapDB.Items.Count == 1 && flg == false)
                {
                    btnDbForward.Enabled = true;
                    btnDbBackward.Enabled = true;
                    btnNextPnl2.Enabled = true;
                    flg = true;
                }

                else
                {
                 
                }
            }
            else
            {
                if (lstMapDB.Items.Count == 1 && flg == true)
                {
                    btnDbBackward.Enabled = true;
                    btnDbForward.Enabled = false;
                }

                else if (lstMapDB.Items.Count == 1 && flg == false)
                {
                    btnDbBackward.Enabled = true;
                    btnDbForward.Enabled = false;
                    btnNextPnl2.Enabled = true;
                }

                else
                { }
            }
        }

        private void txtMappingName_Enter(object sender, EventArgs e)
        {
            btnNextPnl1.Enabled = false;
        }

        private void txtMappingDesc_Enter(object sender, EventArgs e)
        {
            btnNextPnl1.Enabled = true;
        }

    
        private void btnDbBackward_Click(object sender, EventArgs e)
        {
            obj.Btn_DbBackward(lstMapDBs, lstMapDB, btnDbForward, btnDbBackward, btnNextPnl2);
        }

        private void btnPrevPnl2_Click(object sender, EventArgs e)
        {
            lblDisp.Text = "Oracle and SharePoint Mapping Step-1 - Create Mapping Name";
            flg = true;
            flag = true;
            pnlMappingname.Visible = true;
            btnNextPnl1.Enabled = true;
            btnPrevPnl2.Enabled = true;
            pnlDBList.Hide();
        }

        private void btnNextPnl2_Click(object sender, EventArgs e)
        {
            lblDisp.Text = "Oracle and SharePoint Mapping Step-3 - Select SharePoint Mapping List";
            pnl3flag = true;
            pnlDBList.Hide();
            pnlURL.Show();
        }

        private void btnCancelPnl1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnCancelPnl2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txtMappingDesc_TabIndexChanged(object sender, EventArgs e)
        {
            btnNextPnl1.Enabled = true;
            btnNextPnl1.Focus();
        }

        private void txtMappingDesc_Leave(object sender, EventArgs e)
        {
            btnNextPnl1.Focus();
        }

        private void txtMappingName_Leave(object sender, EventArgs e)
        {
            if (txtMappingName.Text == "")
            {
                btnNextPnl1.Enabled = false;
            }
            else
            {
                btnNextPnl1.Enabled = true;
            }

        }

        private void btnPrevPnl3_Click(object sender, EventArgs e)
        {
            lblDisp.Text = "Oracle and SharePoint Mapping Step-2 - Select Oracle Mapping List";
            pnl3flag = false;
            pnlDBList.Show();
            pnlURL.Hide();
        }

    
        private void cmbSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagSites == true)
            {

                ListItemPair oweb = (ListItemPair)cmbSites.SelectedItem;
                obj1.GetLists(lstLibList, cmbSites, objMap.webUrl);
                flagSites = false;
            }
            else
            {
                ListItemPair oweb = (ListItemPair)cmbSites.SelectedItem;
                obj1.GetLists(lstLibList, cmbSites, oweb.URL);
                clr_MapList();
            }
            
        }

        private void clr_MapList()
        {
            lstMapListLib.Items.Clear();
            ListSelectioncnt = 0;
            Libraryselcount = 0;
            Step4Enable();

            if (lstLibList.Items.Count > 0)
            {
                btnListLibAdd.Enabled = true;
            }
            else
            {
                btnListLibAdd.Enabled = false;
            }

        }

        private void btnNextPnl3_Click(object sender, EventArgs e)
        {

            if (flagGv == true)
            {
                MappingInit();
                flagGv = false;
                ListItemPair li = (ListItemPair)cmbSites.SelectedItem;
                lblDestSiteName.Text = li.URL;

                lblDisp.Text = "Oracle and SharePoint Mapping Step-4 - Oracle-SharePoint - Mapping Process";
                pnlURL.Hide();
               
               // Event to ck when edit item table is deleted when the form is processed at the first time
                        
                if (dg1.Rows.Count == 0) { btnNextPnl4.Enabled = false; } else { btnNextPnl4.Enabled = true; }
                pnlGrid.Show();

             
            }
            else
            {
                Pnl4_Tranaction();
            }
            
                      
        }




        private void Pnl4_Tranaction()
        {

            ListItemPair li = (ListItemPair)cmbSites.SelectedItem;
            lblDestSiteName.Text = li.URL;

            lblDisp.Text = "Oracle and SharePoint Mapping Step-4 - Oracle-SharePoint - Mapping Process";
            pnlURL.Hide();
            pnlGrid.Show();

            if (flagMapInit)
            {
                Map_ck_TblView();
                Map_ck_LstLib();
                if (srctblchanged == true || desttblchanged == true)
                {
                    MappingInit();
                    dg1.DataSource = null;
                    AddTableColumn();
                    srctblchanged = false;
                    desttblchanged = false;
                    flagcgintabl = true;
                }
                else
                {
                    //flagcgintabl = false;
                }
            }
           
            else
            {
                AddTableColumn();
                MappingInit();
            }

            if (dg1.Rows.Count == 0) { btnNextPnl4.Enabled = false; } else { btnNextPnl4.Enabled = true; }

        }

        public void Map_ck_TblView()
        {
            foreach (string srctblName in lstMapDB.Items)
            {
                if (!CmbSrcTableName.Items.Contains(srctblName))
                    srctblchanged = true;

            }


            if (lstMapDB.Items.Count != CmbSrcTableName.Items.Count)
            {
                srctblchanged = true;
            }

        }

        public void Map_ck_LstLib()
        {
            try
            {
                foreach (ListItemPair srctblName in lstMapListLib.Items)
                {
                    if (!CmbDestListName.Items.Contains(srctblName))
                        srctblchanged = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            if (lstMapListLib.Items.Count != CmbDestListName.Items.Count)
            {
                desttblchanged = true;
            }

        }

        public void MappingInit()
        {
            CmbDestListName.Items.Clear();
            CmbSrcTableName.Items.Clear();

            CmbDestListName.DisplayMember = "Text";
            foreach (object obj in lstMapListLib.Items)
            {
                CmbDestListName.Items.Add(obj);

            }
            if (CmbDestListName.Items.Count > 0)
                CmbDestListName.SelectedIndex = 0;
            foreach (object obj in lstMapDB.Items)
            {
                CmbSrcTableName.Items.Add(obj);
            }
            if (CmbSrcTableName.Items.Count > 0)
                CmbSrcTableName.SelectedIndex = 0;
        }

        public void AddTableColumn()
        {
            dt = new DataTable();
            dt.Columns.Add("SourceTableName", typeof(string));
            dt.Columns.Add("SourceTableColumn", typeof(string));
            dt.Columns.Add("SourceColumnType", typeof(string));
            dt.Columns.Add("DestinationListName", typeof(string));
            dt.Columns.Add("DestinationListColumn", typeof(string));
            dt.Columns.Add("DestinationColumnType", typeof(string));
            dt.Columns.Add("DestinationListTemplate", typeof(string));
            dg1.DataSource = dt;
        }

        private void btnPrevPnl4_Click(object sender, EventArgs e)
        {
            flagMapInit = true;
            lblDisp.Text = "Oracle and SharePoint Mapping Step-3 - Select SharePoint Mapping List";
            pnlGrid.Hide();
            pnlURL.Show();
        }

        private void btnCancelPnl4_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

     
        public void MoveData(ListBox objListsrc, ListBox objListDest, int val)
        {

            bool adddata1 = false;
            string type1 = "";

            try
            {

                ListItemPair objList = (ListItemPair)objListsrc.SelectedItem;
                if (objList.ListTemplate == Microsoft.SharePoint.Client.BaseType.DocumentLibrary)
                {
                    Libraryselcount = Libraryselcount + val;
                }
                else
                {
                    ListSelectioncnt = ListSelectioncnt + val;
                }

                if (ListSelectioncnt == 0 && Libraryselcount == 1)
                {
                    adddata1 = true;
                    btnListLibAdd.Enabled = true;

                }
                else if (ListSelectioncnt == 1 && Libraryselcount == 0)
                {

                    adddata1 = true;
                    btnListLibAdd.Enabled = true;

                }
                else if (ListSelectioncnt == 2 && Libraryselcount == 0)
                {
                    adddata1 = true;
                    btnListLibAdd.Enabled = false;

                }
                else if (ListSelectioncnt == 1 && Libraryselcount == 1)
                {
                    adddata1 = true;
                    btnListLibAdd.Enabled = false;
                }
                else if (ListSelectioncnt == 0 && Libraryselcount == 0)
                {
                    adddata1 = true;
                    // btnListLibRem.Enabled = false;
                }
                else
                {
                    Libraryselcount = 1;
                    MessageBox.Show("Invalid Mapping.");
                }

                if (adddata1)
                {
                    objListDest.Items.Add(objList);
                    objListDest.DisplayMember = "Text";
                    int selectedIndex = Convert.ToInt32(objListsrc.SelectedIndex);
                    objListsrc.Items.RemoveAt(selectedIndex);
                }
            }
            catch (SystemException ee)
            {
                MessageBox.Show("Select the valid List or Library");
            }


            Step4Enable();
        }

        public enum ObjectState
        {
            Structure,
            Data
        }

        private void CmbSrcTableName_SelectedIndexChanged(object sender, EventArgs e)
        {
            CmbSrcFieldName.Items.Clear();
            obj.LoadObjectData("Data", CmbSrcTableName, CmbSrcFieldName);
        }

        private void CmbDestListName_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListItemPair oweb = (ListItemPair)CmbDestListName.SelectedItem;
            obj1.GetListFields(CmbDestListName, CmbDestFieldName, oweb.URL);
        }

        private void btnAddGrid_Click(object sender, EventArgs e)
        {
            bool exist = false;
            SourceFieldType objsrc = (SourceFieldType)CmbSrcFieldName.SelectedItem;
            ListItemPair dispName = (ListItemPair)CmbDestListName.SelectedItem;
            DestFieldType intName = (DestFieldType)CmbDestFieldName.SelectedItem;

            try
            {
                foreach (DataRow row in dt.Rows)
                {
                   //if (Convert.ToString(row["SourceTableName"]) == Convert.ToString(CmbSrcTableName.SelectedItem) && Convert.ToString(row["SourceTableColumn"]) == Convert.ToString(objsrc.fieldName) && Convert.ToString(row["SourceColumnType"]) == Convert.ToString(lblSrcFieldName.Text) && Convert.ToString(row["DestinationListName"]) == Convert.ToString(dispName.Text) && Convert.ToString(row["DestinationListColumn"]) == Convert.ToString(intName.internalName) && Convert.ToString(row["DestinationColumnType"]) == Convert.ToString(lblDestFieldName.Text))

                   if (Convert.ToString(row["SourceTableName"]) == Convert.ToString(CmbSrcTableName.SelectedItem)
                   && Convert.ToString(row["SourceTableColumn"]) == Convert.ToString(objsrc.fieldName)
                   && Convert.ToString(row["SourceColumnType"]) == Convert.ToString(lblSrcFieldName.Text)
                   && Convert.ToString(row["DestinationListName"]) == Convert.ToString(dispName.Text)
                   && Convert.ToString(row["DestinationListColumn"]) == Convert.ToString(intName.internalName)
                   && Convert.ToString(row["DestinationColumnType"]) == Convert.ToString(lblDestFieldName.Text)
                   && Convert.ToString(row["DestinationListTemplate"]) == Convert.ToString(dispName.ListTemplate))
                    {
                        MessageBox.Show("This Is Duplicate Value");
                        exist = true;
                    }

                   else if (Convert.ToString(row["DestinationListName"]) == Convert.ToString(dispName.Text) && Convert.ToString(row["DestinationListColumn"]) == Convert.ToString(intName.internalName))
                   {
                       MessageBox.Show("Cannot map Destination List Field twice" + System.Environment.NewLine + "'"+ row["DestinationListColumn"] +"'  Field already mapped", "Existing", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                       exist = true;
                   }


                }

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
            


            //if (!exist)
            //{
            //    dt.Rows.Add(CmbSrcTableName.SelectedItem, objsrc.fieldName, lblSrcFieldName.Text, dispName.Text, intName.internalName, lblDestFieldName.Text, Convert.ToString(dispName.ListTemplate));
            //    dg1.DataSource = dt;
            //}
            //btnNextPnl4.Enabled = true;
            if (!exist)
            {
                if (string.Equals(lblDestFieldName.Text, "File", StringComparison.CurrentCultureIgnoreCase))
                {
                    DialogResult result = MessageBox.Show("Could you please ensure '" + objsrc.fieldName + "' consist URL(Document Path). Since you have selected Document Libray", "Document Library Verification", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.OK)
                    {
                        dt.Rows.Add(CmbSrcTableName.SelectedItem, objsrc.fieldName, lblSrcFieldName.Text, dispName.Text, intName.internalName, lblDestFieldName.Text, Convert.ToString(dispName.ListTemplate));
                        dg1.DataSource = dt;
                    }
                }
                else
                {
                    dt.Rows.Add(CmbSrcTableName.SelectedItem, objsrc.fieldName, lblSrcFieldName.Text, dispName.Text, intName.internalName, lblDestFieldName.Text, Convert.ToString(dispName.ListTemplate));
                    dg1.DataSource = dt;
                }
            }
            if (dg1.Visible)
            {
                btnNextPnl4.Enabled = true;
            }
        }

        private void dg1_MouseClick(object sender, MouseEventArgs e)
        {
            if (mode == "View")
            {
            }
            else
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    if (dg1.Rows.Count > 0)
                    {
                        contextMenuStrip1.Show(Cursor.Position);
                    }
                }
            }
        }

        private void deleteSelectedRowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are You want to Delete Selected Row ?", "Row Selection", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                dg1.Rows.Remove(dg1.CurrentRow);
            }
            if (dg1.Rows.Count == 0) { btnNextPnl4.Enabled = false; } else { btnNextPnl4.Enabled = true; }
        }

        private void btnNextPnl4_Click(object sender, EventArgs e)
        {
            OSP_MappingProcess();
        }

        private void OSP_MappingProcess()
        {
            lblDisp.Text = "Oracle and SharePoint Mapping Step-5 - Oracle-Data Access";
            pnlGridBind.Show();
            pnlGrid.Hide();
            if (rdBtnTables.Checked == true && obj.cnt == 1)
            {
                gvBind.DataSource = null;
                rdBtnAll.Enabled = true;
                rdBtnAll.Checked = true;
                rdBtnQuery.Enabled = false;
                txtQuery.Enabled = false;
                txtQuery.Text = "";
                btnExecute.Enabled = false;
                string strQry = "select * from " + CmbSrcTableName.Text;
                obj.LoadOrcData(strQry, gvBind);
            }
            else if (rdBtnTables.Checked == true && obj.cnt == 2)
            {
                if (flagcgintabl == false)
                {
                    txtQuery.Text = txtQuery.Text;
                    rdBtnAll.Enabled = false;
                    rdBtnAll.Checked = false;
                    rdBtnQuery.Enabled = true;
                    rdBtnQuery.Checked = true;
                    txtQuery.Enabled = true;
                    btnExecute.Enabled = true;
                }
                else
                {
                   
                    txtQuery.Visible = true;
                    txtQuery.Enabled = true;
                    txtQuery.Text = "";
                    gvBind.DataSource = null;
                    rdBtnAll.Enabled = false;
                    rdBtnAll.Checked = false;
                    rdBtnQuery.Enabled = true;
                    rdBtnQuery.Checked = true;
                    btnExecute.Visible = true;
                    btnExecute.Enabled = true;
                    flagcgintabl = false;
                   
                }
                //gvBind.DataSource = null;
                //rdBtnAll.Enabled = false;
                //rdBtnAll.Checked = false;
                //rdBtnQuery.Enabled = true;
                //rdBtnQuery.Checked = true;
                //txtQuery.Enabled = true;
                //btnExecute.Enabled = true;

            }
            else if (rdBtnTables.Checked == true && obj.cnt == 0)
            {
                if (mode == "View")
                {
                    if (lstMapDB.Items.Count == 2)
                    { 
                        rdBtnQuery.Enabled = false;
                        rdBtnQuery.Checked = true;
                        btnExecute.Enabled = false;
                        txtQuery.Enabled = false;
                    }
                    else
                    {
                        rdBtnAll.Enabled = false; 
                        rdBtnAll.Checked = true;
                        btnExecute.Visible  = false;
                        txtQuery.Visible = false;
                    }
                }
               
                else
                {

                    if (lstMapDB.Items.Count == 2)
                    {
                        rdBtnQuery.Enabled = true;
                        rdBtnQuery.Checked = true;
                        rdBtnAll.Enabled = false;
                        btnExecute.Enabled = true;
                        txtQuery.Enabled = true;
                    }
                    else
                    {
                        rdBtnAll.Enabled = true;
                        rdBtnQuery.Enabled = false;
                        btnExecute.Enabled = false;
                        txtQuery.Enabled = false;
                    }
                                
                }

                //rdBtnAll.Enabled = false; txtQuery.Enabled = false;
                //if (lstMapDB.Items.Count == 2)
                //{
                //    rdBtnQuery.Checked = true;
                //}
                //else
                //{ 
                //    rdBtnAll.Checked = true; 
                //}
                

                //rdBtnQuery.Enabled = false;
                //rdBtnQuery.Checked = false;
                ////txtQuery.Enabled = true;
                //btnExecute.Enabled = true;

            }
            else if (rdBtnQuery.Checked == true && obj.cnt == 0)
            {
                rdBtnAll.Enabled = false;
                rdBtnAll.Checked = false;
                rdBtnQuery.Enabled = true;
                rdBtnQuery.Checked = true;
                txtQuery.Enabled = true;
                btnExecute.Enabled = true;

            }

            else if (rdBtnViews.Checked == true && obj.cnt == 1)
            {
                rdBtnAll.Enabled = true;
                rdBtnAll.Checked = true;
                rdBtnQuery.Enabled = false;
                rdBtnQuery.Checked = false;
                txtQuery.Text="";
                txtQuery.Enabled = false;
                btnExecute.Enabled = false;
                string strQry = "select * from " + CmbSrcTableName.Text;
                obj.LoadOrcData(strQry, gvBind);

            }
            else if (rdBtnViews.Checked == true && obj.cnt == 0)
            {
                //rdBtnAll.Enabled = true;
                //rdBtnAll.Checked = true;
                //rdBtnQuery.Enabled = false;
                //rdBtnQuery.Checked = false;
                //txtQuery.Enabled = true;
                //btnExecute.Enabled = true;
                gvBind.DataSource = null;
                if (mode == "View")
                {
                    rdBtnAll.Enabled = false;
                }
                else
                {
                    rdBtnAll.Enabled = true;
                }
                rdBtnAll.Checked = true;
                rdBtnQuery.Enabled = false;
                txtQuery.Enabled = false;
                txtQuery.Text = "";
                btnExecute.Enabled = false;
                string strQry = "select * from " + CmbSrcTableName.Text;
                obj.LoadOrcData(strQry, gvBind);

            }
            else
            {
                txtQuery.Text = "";
                rdBtnAll.Enabled = false;
                rdBtnQuery.Enabled = true;
                rdBtnQuery.Checked = true;
                txtQuery.Enabled = true;
                btnExecute.Enabled = true;
                gvBind.DataSource = null;

            }

            if (gvBind.DataSource == null) { btnNextPnl5.Enabled = false; } else { btnNextPnl5.Enabled = true; }
        }

        private void btnCancelPnl5_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnPrevPnl5_Click(object sender, EventArgs e)
        {
            lblDisp.Text = "Oracle and SharePoint Mapping Step-4 - Oracle-SharePoint - Mapping Process";
            pnlGrid.Show();
            pnlGridBind.Hide();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            Execute_Query();
            if (gvBind.DataSource == null) { btnNextPnl5.Enabled = false; } else { btnNextPnl5.Enabled = true; }
        }

        private void Execute_Query()
        {
            try
            {
                int cnt = 0;
                if (txtQuery.Text.Trim() != "")
                {
                    if (txtQuery.Text.Trim().ToUpper().Contains(" FROM "))
                    {
                        if (rdBtnTables.Checked && obj.cnt == 2)
                        {

                            string QRY = txtQuery.Text.ToUpper().Substring(txtQuery.Text.ToUpper().LastIndexOf(" FROM "));
                            string wHQRY = QRY.Substring(QRY.LastIndexOf(" FROM "), QRY.LastIndexOf(" WHERE ")).Replace(" FROM ", "").Trim();
                            string[] tbls = wHQRY.Split(',');
                            foreach (string tbl in tbls)
                            {
                                string strCk;
                                strCk = tbl.Split(' ')[0];
                                {
                                    foreach (string li in lstMapDB.Items)
                                    {
                                        if (strCk == li)
                                        {
                                            cnt++;
                                        }
                                    }
                                }
                            }
                            if (cnt == 2)
                            {
                                obj.LoadOrcData(txtQuery.Text, gvBind);
                            }
                            else
                            {
                                gvBind.DataSource = null;
                                MessageBox.Show("You cannot configure with other table or Invalid table name");
                            }
                        }
                        else
                        {
                            obj.LoadOrcData(txtQuery.Text, gvBind);
                            //view coding
                        }
                    }

                    else
                    {
                        obj.LoadOrcData(txtQuery.Text, gvBind);
                    }

                }

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + System.Environment.NewLine + "Or" + System.Environment.NewLine + "Pls check your query");
                gvBind.DataSource = null;
            }

        }

        private void btnNextPnl5_Click(object sender, EventArgs e)
        {
            if (mode == "View")
            {
                btnCancelPnl6.Visible = false;
                btnSave.Text = "Ok";
                lblDisp.Text = "Oracle and SharePoint Mapping Step-6 - Oracle-To-SharePoint-Posting";
                pnlIntegrate.Show();
                pnlGridBind.Hide();
                Mapping_Integrate();
            }
            else
            {
                lblDisp.Text = "Oracle and SharePoint Mapping Step-6 - Oracle-To-SharePoint-Posting";
                pnlIntegrate.Show();
                pnlGridBind.Hide();
                Mapping_Integrate();
            }
        }

        private void btnPrevPnl6_Click(object sender, EventArgs e)
        {
            lblDisp.Text = "Oracle and SharePoint Mapping Step-5 - Oracle-Data Access";
            pnlGridBind.Show();
            pnlIntegrate.Hide();
        }

        public void Mapping_Integrate()
        {
            lblMapName.Text = txtMappingName.Text.ToUpper();
            lblMapDesc.Text = txtMappingDesc.Text.ToUpper();

            lblSrcTblView.Text = "";
            foreach (string srctblName in lstMapDB.Items)
            {
                string s = srctblName.ToUpper();
                lblSrcTblView.Text = lblSrcTblView.Text + System.Environment.NewLine + s.ToString();
            }

            lblDestListLib.Text = "";
            foreach (ListItemPair destListName in lstMapListLib.Items)
            {
                string s = destListName.Text.ToUpper();
                lblDestListLib.Text = lblDestListLib.Text + System.Environment.NewLine + s.ToString();
            }


            lblMapRowCnt.Text = Convert.ToString(dg1.Rows.Count);
            lblSrcRecCnt.Text = Convert.ToString(gvBind.Rows.Count);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (mode == "View")
            {
                this.Close();
            }
            else
            {
                if (rdBtnAll.Checked && !rdBtnViews.Checked)
                {

                    DialogResult result = MessageBox.Show("Are you sure you want to update the record ?", "Row Updating", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        objMap.Mapping_List(txtMappingName.Text, txtMappingDesc.Text, Convert.ToString(rdBtnAll.Checked), txtQuery.Text, "", mapid,"");
                        objMap.Mapping_Column_List(dt, objMap.SiteUrl, mapid);
                        MessageBox.Show("Mapping Updated");
                        this.Close();
                    }

                    
                }
                else if (rdBtnQuery.Checked && obj.cnt == 2)
                {
                    DialogResult result = MessageBox.Show("Are you sure you want to update the record ?", "Row Updating", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        objMap.Mapping_List(txtMappingName.Text, txtMappingDesc.Text, Convert.ToString(rdBtnAll.Checked), txtQuery.Text, "", mapid, "");
                        objMap.Mapping_Column_List(dt, objMap.SiteUrl, mapid);
                        MessageBox.Show("Mapping Updated");
                        this.Close();
                    }
                }
                else
                {
                    DialogResult result = MessageBox.Show("Are you sure you want to update the record ?", "Row Updating", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        objMap.Mapping_List(txtMappingName.Text, txtMappingDesc.Text, Convert.ToString(rdBtnAll.Checked), txtQuery.Text, CmbSrcTableName.Text, mapid,"");
                        objMap.Mapping_Column_List(dt, objMap.SiteUrl, mapid);
                        MessageBox.Show("Mapping Updated");
                        this.Close();
                    }
                }
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btnPrevPnl7_Click(object sender, EventArgs e)
        {
            pnlIntegrate.Show();
           // pnlDataMigrate.Hide();

        }
              

        private void btnGo_Click(object sender, EventArgs e)
        {
            obj1.GetSubSites(cmbSites, txtUrl.Text);
            if (cmbSites.Items.Count > 0)
            {
                cmbSites.SelectedIndex = 0;
            }
        }

        private void btnListLibAdd_Click(object sender, EventArgs e)
        {
            if (lstLibList.Text == "")
                MessageBox.Show("Please Select List / Library");
            else
                MoveData(lstLibList, lstMapListLib, 1);
        }

        private void btnListLibRem_Click(object sender, EventArgs e)
        {
            MoveData(lstMapListLib, lstLibList, -1);
        }

        private void btnCancelPnl3_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void CmbSrcFieldName_SelectedIndexChanged(object sender, EventArgs e)
        {
            obj.LoadObjectType("Data", CmbSrcFieldName, lblSrcFieldName);
        }

        private void CmbDestFieldName_SelectedIndexChanged(object sender, EventArgs e)
        {
            obj1.GetListFieldType(CmbDestFieldName, lblDestFieldName);
        }

        private void deleteSelectedRowToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are You want to Delete Selected Row ?", "Row Selection", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                dg1.Rows.Remove(dg1.CurrentRow);
            }
            if (dg1.Rows.Count == 0) { btnNextPnl4.Enabled = false; } else { btnNextPnl4.Enabled = true; }
        }

        private void btnCancelPnl6_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnDbForward_Click(object sender, EventArgs e)
        {
            if (rdBtnTables.Checked)
            {
                obj.Btn_DbForward(rdBtnTables, lstMapDBs, lstMapDB, btnDbForward, btnDbBackward, btnNextPnl2);

            }
            else
            {
                obj.Btn_DbForward(rdBtnViews, lstMapDBs, lstMapDB, btnDbForward, btnDbBackward, btnNextPnl2);
            }
                
        }

        private void rdBtnTables_Click(object sender, EventArgs e)
        {
            if (pnl3flag)
            {
                
            }
            else
            {
                btnDbBackward.Enabled = false;
                btnNextPnl2.Enabled = false;
                obj.Lst_Tbl_Rec(rdBtnTables, btnDbForward, lstMapDBs, lstMapDB, flag);
            }
        }

        private void rdBtnViews_CheckedChanged(object sender, EventArgs e)
        {
            if (pnl3flag)
            {

            }
            else
            {
                if (flagview == true)
                {
                    flagview = false;
                }
                else 
                {
                    btnDbBackward.Enabled = false;
                    obj.Lst_View_Rec(rdBtnViews, btnDbForward, lstMapDBs, lstMapDB, flag);
                }
                
            }
        }

        private void btnCancelPnl7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMappingName_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (txtMappingName.Text == "")
            //{
            //    btnNextPnl1.Enabled = false;
            //}
            //else
            //{
            //    btnNextPnl1.Enabled = true;
            //}
        }

        private void pnlMappingname_MouseMove(object sender, MouseEventArgs e)
        {
            if (txtMappingName.Text == "")
            {
                btnNextPnl1.Enabled = false;
            }
            else
            {
                btnNextPnl1.Enabled = true;
            }

        }

  
    }
}

